package com.mlnx.web.ui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mlnx.common.entity.Response;
import com.mlnx.service.common.enums.ResponseEnum;
import com.mlnx.ui.entity.User;
import com.mlnx.ui.entity.UserRole;
import com.mlnx.ui.pojo.form.UserForm;
import com.mlnx.ui.service.RoleService;
import com.mlnx.ui.service.UserRoleService;
import com.mlnx.ui.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "UI用户接口", tags = {"UI用户接口"})
@RestController
@RequestMapping("ui/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "通过用户名获取用户信息")
    @GetMapping(value = "/{name}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", required = true)
    })
    public Response getUserByName(@PathVariable("name") String name) {
        return result(userService.getOne(new QueryWrapper<User>().eq("name", name)));
    }

    @ApiOperation(value = "注册账号")
    @PostMapping(value = "")
    public Response register(@Valid UserForm userForm) {
        User user = userService.getOne(new QueryWrapper<User>().eq("name", userForm.getName()));
        // 验证账号是否已被注册
        if (user != null)
            return result(ResponseEnum.UserExisted);

        user = new User();
        user.setName(userForm.getName());
        user.setPassword(userForm.getPassword());
        userService.save(user);
        UserRole userRole =new UserRole();
        userRole.setRoleId(2);
        userRole.setUserId(user.getId());
        userRoleService.save(userRole);
        return result();
    }

    @ApiOperation(value = "超级管理员绑定角色和用户")
    @GetMapping(value = "bind")
    public Response bindUserRole(@RequestParam("userId")Integer userId,@RequestParam("userId")Integer roleId) {
        UserRole one = userRoleService.getOne(new QueryWrapper<UserRole>().eq("user_id", userId).eq("'status", 0));
        if(one !=null){
            return result("该用户已经分配角色");
        }
        UserRole userRole =new UserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(userId);
        userRoleService.save(userRole);
      return result();
    }

    @ApiOperation(value = "显示所有角色信息")
    @GetMapping(value = "find/role")
    public Response findRole(@RequestParam("userId")Integer userId,@RequestParam("userId")Integer roleId) {
        UserRole one = userRoleService.getOne(new QueryWrapper<UserRole>().eq("user_id", userId).eq("'status", 0));
        if(one !=null){
            return result("该用户已经分配角色");
        }
        UserRole userRole =new UserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(userId);
        userRoleService.save(userRole);
        return result();
    }


}
